/**
 * 
 */
Ext.ns("NavigationGPS");
NavigationGPS.init = function() {
	NavigationGPS.map;
	var comments;
	var marker;
	var geocoder;
	var route;
	var city;
	var region;
	var address;
	NavigationGPS.DirectionDisplay;
	NavigationGPS.DirectionsService = new google.maps.DirectionsService();
	NavigationGPS.PS_H;
	NavigationGPS.PS_V;
	NavigationGPS.StartPosition;
	NavigationGPS.StartMessage;
	NavigationGPS.StartMarker;
	NavigationGPS.EndPosition;
	NavigationGPS.EndMessage;
	NavigationGPS.EndMarker;
	NavigationGPS.TravelMode;
	NavigationGPS.select = new Ext.grid.CheckboxSelectionModel({
				handleMouseDown : Ext.emptyFn
			});
	NavigationGPS.cm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(),
			NavigationGPS.select, {
				header : '考场名称',
				dataIndex : 'exma_Name',
				sortable : true,
				width : 160
			}, {
				header : '考点名称',
				dataIndex : 'place_Name',
				sortable : true,
				width : 80
			}, {
				header : '考场名称',
				dataIndex : 'position_Name',
				sortable : true,
				width : 80
			}]);
	NavigationGPS.store = new Ext.data.JsonStore({
				url : 'fixPosition.action',
				totalProperty : 'totalRows',
				root : 'exs',
				fields : [{
							name : "exam_Id",
							type : 'int'
						}, {
							name : "exma_Name",
							type : 'string'
						}, {
							name : "place_Name",
							type : 'string'
						}, {
							name : "position_Name",
							type : 'string'
						}, {
							name : "p_H",
							type : 'float'
						}, {
							name : "p_V",
							type : 'float'
						}]
			});
	NavigationGPS.Panel = new Ext.grid.GridPanel({
		sm : NavigationGPS.select,
		cm : NavigationGPS.cm,
		store : NavigationGPS.store,
		viewConfig : {
			columnsText : '选择显示列',
			forceFit : true
		},
		autoScroll : true,
		loadMask : true,
		height : 400,
		frame : true,
		tbar : new Ext.Toolbar(['-', {
			text : '确认',
			handler : function() {
				var rows = NavigationGPS.Panel.getSelectionModel()
						.getSelections();
				if (rows.length > 1 || rows.length == 0) {
					Ext.Msg.show({
								title : '提示',
								msg : '请选择一个考试位置进行定位！',
								buttons : Ext.MessageBox.OK,
								icon : Ext.MessageBox.WARNING
							});
					return false;
				} else {
					Ext.each(rows, function(item) {
								NavigationGPS.EndPosition = new google.maps.LatLng(
										item.data.p_H, item.data.p_V);
								NavigationGPS.EndMarker = new google.maps.Marker(
										{
											map : NavigationGPS.map,
											draggable : true,
											clickable : false
										});
								NavigationGPS.EndMarker
										.setTitle(item.data.exma_Name + "考点位置："
												+ item.data.place_Name + ","
												+ item.data.position_Name);
								NavigationGPS.map.setCenter(NavigationGPS.EndPosition);
								NavigationGPS.EndMarker
										.setPosition(NavigationGPS.EndPosition);
								NavigationGPS.map.setZoom(13);
							});
					NavigationGPS.Window.hide();

				}
			}
		}, '-', {
			text : '取消',
			handler : function() {
				NavigationGPS.Panel.getSelectionModel().clearSelections();
			}
		}])
	});
	NavigationGPS.Window = new Ext.Window({
				region : "center",
				id : "NavigationGPSWindow",
				title : '考试位置确定窗口',
				deferredRender : true,
				closeAction : 'hide',
				loadMask : true,
				width : 600,
				height : 400,
				closable : true,
				modal : true,
				progress : true,
				draggable : true,
				resizable : false,
				viewConfig : {
					forceFit : true
				},
				items : [NavigationGPS.Panel]
			});
	Ext.Ajax.request({
				url : 'searchPosition.action',
				method : 'post',
				success : function(response) {
					var obj = Ext.decode(response.responseText);
					if (obj.success == false) {
						return false;
					} else {
						NavigationGPS.PS_V = obj.position_V;
						NavigationGPS.PS_H = obj.position_H;
					}
				}
			});
}
Ext.onReady(NavigationGPS.init);
function initialize() {
	geocoder = new google.maps.Geocoder();
	NavigationGPS.DirectionDisplay = new google.maps.DirectionsRenderer();
	var myOptions = {
		zoom : 16, // 缩放级别
		// 可以使用
		// 0（最低缩放级别，在地图上可以看到整个世界）到
		// 19（最高缩放级别，可以看到独立建筑物）之间的缩放级别
		mapTypeId : google.maps.MapTypeId.ROADMAP, // ROADMAP -
		// 默认视图
		// SATELLITE-显示Google地球卫星图像
		// HYBRID-混合显示普通视图和卫星视图
		// TERRAIN-地形图
		scaleControl : true,// 比例尺
		mapTypeControl : true,
		streetViewControl : true
	};

	NavigationGPS.map = new google.maps.Map(document.getElementById('maps'), myOptions);
	NavigationGPS.DirectionDisplay.setMap(NavigationGPS.map);
	NavigationGPS.DirectionDisplay.setPanel(document
			.getElementById("directions"));
	NavigationGPS.StartMarker = new google.maps.Marker({
				map :NavigationGPS.map,
				draggable : true,
				clickable : false
			});
	google.maps.event.addListener(NavigationGPS.StartMarker, "dragend",
			function(event) {
				var latLng = event.latLng;
				if (geocoder) {
					geocoder.geocode({
								'latLng' : latLng
							}, function(results, status) {
								if (status == google.maps.GeocoderStatus.OK) {
									if (results[0]) {
										NavigationGPS.StartMarker
												.setPosition(latLng);
										NavigationGPS.StartMarker
												.setTitle(results[0].formatted_address);
									}
								} else {
									alert("导航分析出现问题: " + status);
								}
							});
				}
			});
	if (navigator.geolocation) {
		navigator.geolocation.getCurrentPosition(function(position) {
					NavigationGPS.StartPosition = new google.maps.LatLng(
							position.coords.latitude, position.coords.longitude);
					NavigationGPS.StartMarker
							.setPosition(NavigationGPS.StartPosition);
					if (position.address != null) {
						NavigationGPS.StartMessage = "您当前定位的所在国家:"
								+ position.address.country + ",城市:"
								+ position.address.region + ",地区: "
								+ position.address.city + ",所在街道:"
								+ position.address.street
								+ position.address.streetNumber;
					} else {
						NavigationGPS.StartMessage = "您当前的被移动定位的位置";
					}
					NavigationGPS.StartMarker
							.setTitle(NavigationGPS.StartMessage);
					var infowindow = new google.maps.InfoWindow({
								map : NavigationGPS.map,
								position : NavigationGPS.StartPosition,
								content : NavigationGPS.StartMessage
							});
					NavigationGPS.map.setCenter(NavigationGPS.StartPosition);
				}, function() {
					handleNoGeolocation(true);
				});
	} else {
		handleNoGeolocation(false);
	}
}
function searchLocation() {
	var message = document.getElementById('position').value;
	if (geocoder) {
		geocoder.geocode({
					'address' : message
				}, function(results, status) {
					if (status == google.maps.GeocoderStatus.OK) {
						if (results[0]) {
							NavigationGPS.StartPosition = results[0].geometry.location;
							NavigationGPS.StartMarker.setTitle(message);
							NavigationGPS.map.setCenter(NavigationGPS.StartPosition);
							NavigationGPS.StartMarker
									.setPosition(NavigationGPS.StartPosition);
							NavigationGPS.map.setZoom(16);
						} else {
						}
					} else {
						alert("对不起，你输入的信息不正确，以至于无法导航！ ");
					}
				});
	}
}
function searchLocation2() {
	var message = document.getElementById('position2').value;
	NavigationGPS.EndMarker = new google.maps.Marker({
				map : NavigationGPS.map,
				draggable : true,
				clickable : false
			});
	if (geocoder) {
		geocoder.geocode({
					'address' : message
				}, function(results, status) {
					if (status == google.maps.GeocoderStatus.OK) {
						if (results[0]) {
							NavigationGPS.EndPosition = results[0].geometry.location;
							NavigationGPS.EndMarker.setTitle(message);
							NavigationGPS.map.setCenter(NavigationGPS.EndPosition);
							NavigationGPS.EndMarker
									.setPosition(NavigationGPS.EndPosition);
							NavigationGPS.map.setZoom(13);
						} else {
						}
					} else {
						alert("对不起，你输入的信息不正确，以至于无法导航！ ");
					}
				});
	}
}
function handleNoGeolocation(errorFlag) {
	if (errorFlag) {
		var content = '对不起导航失败！';
	} else {
		var content = '对不起，您使用的浏览器不支持导航系统，请选择谷歌，火狐以及Safari支持Html5的浏览器!';
	}
	var options = {
		map : NavigationGPS.map,
		content : address
	};
	var infowindow = new google.maps.InfoWindow(options);
	NavigationGPS.map.setCenter(options.position);
}
function calcRoute() {
	var way = document.getElementById("selectWay").value;
	if (way == 1) {
		NavigationGPS.TravelMode = google.maps.DirectionsTravelMode.DRIVING;
	} else if (way == 2) {
		NavigationGPS.TravelMode = google.maps.DirectionsTravelMode.WALKING;
	}
	var request = {
		origin : NavigationGPS.StartPosition,
		destination : NavigationGPS.EndPosition,
		travelMode : NavigationGPS.TravelMode,
		provideRouteAlternatives : true
	};
	NavigationGPS.DirectionsService.route(request, function(result, status) {
				if (status == google.maps.DirectionsStatus.OK) {
					NavigationGPS.DirectionDisplay.setDirections(result);
				}
			});
}
function locationSelf() {
	NavigationGPS.StartPosition = new google.maps.LatLng(NavigationGPS.PS_H,
			NavigationGPS.PS_V);
	NavigationGPS.map.setCenter(NavigationGPS.StartPosition);
	NavigationGPS.StartMarker.setPosition(NavigationGPS.StartPosition);
}
function selectExam() {
	NavigationGPS.Window.show();
	NavigationGPS.store.load();
}